package com.beetlestech.nfe.main.biz.com.impl;

import java.util.Date;
import java.util.Properties;
import java.util.UUID;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.beetlestech.nfe.fw.core.biz.AbstractDBLogic;
import com.beetlestech.nfe.main.biz.com.HbEmailRegister;
import com.beetlestech.nfe.main.biz.constant.CommonConstant;
import com.beetlestech.nfe.main.biz.constant.CtgCodeConstant;
import com.beetlestech.nfe.main.biz.constant.HbMessageCode;
import com.beetlestech.nfe.main.db.constant.SQLConstant;
import com.beetlestech.nfe.main.db.tbl.EmailactiveDTO;
import com.beetlestech.nfe.main.db.tbl.MststudentDTO;
import com.beetlestech.nfe.main.db.tbl.MstusrDTO;

public class HbEmailRegisterImpl extends AbstractDBLogic implements HbEmailRegister, CtgCodeConstant, HbMessageCode, CommonConstant, SQLConstant {
	public static final String HOST = "smtp.sina.net";
	public static final String PROTOCOL = "smtp";
	public static final int PORT = 25;
	public static final String FROM = "dalian@xcjacca.com";// 发件人的email
	public static final String PWD = "Xcj82556381";

	/*public class SendEmail {
		public static final String HOST = "smtp.beetlestech.com";
	    public static final String PROTOCOL = "smtp";  
	    public static final int PORT = 25;
	    public static final String FROM = "maxiaohua@beetlestech.com";//发件人的email
	    public static final String PWD = "Nhy65tgb";//发件人密码

	    *//**
	     * 获取Session
	     * @return
	     *//*
	    private Session getSession() {
	        Properties props = new Properties();
	        props.put("mail.smtp.host", HOST);//设置服务器地址
	        props.put("mail.store.protocol" , PROTOCOL);//设置协议
	        props.put("mail.smtp.port", PORT);//设置端口
	        props.put("mail.smtp.auth" , true);

	        Authenticator authenticator = new Authenticator() {

	            @Override
	            protected PasswordAuthentication getPasswordAuthentication() {
	                return new PasswordAuthentication(FROM, PWD);
	            }

	        };
	        Session session = Session.getDefaultInstance(props , authenticator);

	        return session;
	    }

	    public  void send(String toEmail , String content) {
	        Session session = getSession();
	        try {
	            System.out.println("--send--"+content);
	            // Instantiate a message
	            Message msg = new MimeMessage(session);

	            //Set message attributes
	            msg.setFrom(new InternetAddress(FROM));
	            InternetAddress[] address = {new InternetAddress(toEmail)};
	            msg.setRecipients(Message.RecipientType.TO, address);
	            msg.setSubject("账号激活邮件");
	            msg.setSentDate(new Date());
	            msg.setContent(content , "text/html;charset=utf-8");

	            //Send the message
	            Transport.send(msg);
	        }
	        catch (MessagingException mex) {
	            mex.printStackTrace();
	        }
	    }


	}*/


	@Override
	public void insertUserInfo(String email, String userName, String passWord) {
		checkUserInfo(email,userName);
		MstusrDTO mstusrDTO =new MstusrDTO(); 
		mstusrDTO.setemail(email);
		mstusrDTO.setusername(userName);
		mstusrDTO.setpassword(passWord);
		mstusrDTO.settype(0);
		mstusrDTO.setstate(0);
		mstusrDTO.setfirstloginflg(0);
		mstusrDTO.setlastdate(getCurrentDate());
		mstusrDTO.setlasttime(getCurrentTime());
		updateDAO.updateOne(MstusrDTO.INSERT,mstusrDTO,M10040WS);
		MststudentDTO mststudentDTO=new MststudentDTO();
	    mststudentDTO.setusrid(mstusrDTO.getusrid());
	    mststudentDTO.setlastdate(getCurrentDate());
	    mststudentDTO.setlasttime(getCurrentTime());
	    updateDAO.updateOne(MststudentDTO.INSERT,mststudentDTO,M10040WS);
	    UUID uuid = UUID.randomUUID();
	    String activeCode =uuid.toString();
	    EmailactiveDTO emailactiveDTO=new EmailactiveDTO();
	    emailactiveDTO.setactiveCode(activeCode);
	    emailactiveDTO.setuserid(mstusrDTO.getusrid());
	    updateDAO.updateOne(EmailactiveDTO.INSERT, emailactiveDTO, M10040WS);
	    StringBuffer sb=new StringBuffer("感谢您注册A课堂！</br>请点击下面链接激活您的A课堂账号！</br>");
        sb.append("<a href=\"http://class.xcjacca.com/NFE/emailActiveAction.do?&activeCode=");
        sb.append(activeCode);
        sb.append("\">http://class.xcjacca.com/NFE/emailActiveAction.do?&activeCode=");
        sb.append(activeCode);
        sb.append("</a>");
        /*SendEmail sendEmail=new SendEmail();
        sendEmail.send(activeCode, sb.toString());*/
        sendEmail(email, sb.toString());
	}
	
	private void checkUserInfo(String email, String userName) {
		MstusrDTO mstusrDTO =new MstusrDTO(); 
		mstusrDTO.setemail(email);
		if(queryDAO.count(MstusrDTO.COUNT, mstusrDTO) > 0){
			createApplicationException(M10060WC);
		}else{
			mstusrDTO.setemail(null);
			mstusrDTO.setusername(userName);;
			if(queryDAO.count(MstusrDTO.COUNT, mstusrDTO) > 0){
				createApplicationException(M10070WC);
			}
		}
	}
	private void sendEmail(String toEmail , String content){
		Session session = getSession();
        try {
            System.out.println("--send--"+content);
            // Instantiate a message
            Message msg = new MimeMessage(session);

            //Set message attributes
            msg.setFrom(new InternetAddress(FROM));
            InternetAddress[] address = {new InternetAddress(toEmail)};
            msg.setRecipients(Message.RecipientType.TO, address);
            msg.setSubject("A课堂");
            msg.setSentDate(new Date());
            msg.setContent(content , "text/html;charset=utf-8");

            //Send the message
            Transport.send(msg);
        }
        catch (MessagingException mex) {
            mex.printStackTrace();
        }
    }
		
	
	private Session getSession() {
        Properties props = new Properties();
        props.put("mail.smtp.host", HOST);//设置服务器地址
        props.put("mail.store.protocol" , PROTOCOL);//设置协议
        props.put("mail.smtp.port", PORT);//设置端口
        props.put("mail.smtp.auth" , true);
        Authenticator authenticator = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(FROM, PWD);
            }

        };
        Session session = Session.getDefaultInstance(props , authenticator);

        return session;
    }
	
}
